package com.github.nuggets.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.nuggets.model.Document;
import com.github.nuggets.model.entity.NuggetsDocumentEntity;
import com.github.nuggets.model.entity.State;
import com.github.nuggets.model.exception.BizException;
import com.github.nuggets.model.param.DocumentSaveParams;
import com.github.nuggets.model.pojo.LoginAccountInfo;
import com.github.nuggets.model.result.PageResult;
import com.github.nuggets.repository.DocumentRepository;
import com.github.nuggets.service.DocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/22 5:44 下午
 */
@Service
@Slf4j
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    private DocumentRepository documentRepository;

    @Override
    public Boolean releaseDocument(LoginAccountInfo loginAccount, DocumentSaveParams params) {

        //根据参数获取文章entity
        NuggetsDocumentEntity documentEntity = new NuggetsDocumentEntity();
        BeanUtil.copyProperties(params, documentEntity);

        documentEntity
                .setAccountId(loginAccount.getId())
                .setState(State.CHECKING);


        return documentRepository.save(documentEntity);

    }

    @Override
    public Boolean deleteDocument(LoginAccountInfo loginAccount, Long documentId) {

        NuggetsDocumentEntity documentEntity = documentRepository.getDocumentByAccount(loginAccount.getId(), documentId)
                .orElseThrow(() -> new BizException("未找到该文章"));

        return documentRepository.removeById(documentEntity);
    }

    @Override
    public Boolean updateDocument(LoginAccountInfo loginAccount, Long documentId, DocumentSaveParams params) {
        NuggetsDocumentEntity documentEntity = documentRepository.getDocumentByAccount(loginAccount.getId(), documentId)
                .orElseThrow(() -> new BizException("未找到该文章"));

        BeanUtil.copyProperties(params, documentEntity);

        //重新修改为审核状态
        documentEntity.setState(State.CHECKING);

        return documentRepository.updateById(documentEntity);
    }

    @Override
    public Document getLoginAccountDocument(LoginAccountInfo loginAccount, Long documentId) {
        NuggetsDocumentEntity documentEntity = documentRepository.getDocumentByAccount(loginAccount.getId(), documentId)
                .orElseThrow(() -> new BizException("未找到该文章"));

        Document document = new Document();
        BeanUtil.copyProperties(documentEntity, document);

        log.info(document.getContext());

        return document;
    }

    @Override
    public PageResult<Document> listLoginAccountDocument(LoginAccountInfo loginAccount, Integer state, Integer pageIndex, Integer pageSize) {
        IPage<NuggetsDocumentEntity> entityPageResult = documentRepository.listDocumentByAccount(loginAccount.getId(), State.getByCode(state), pageIndex, pageSize);


        List<Document> documents = entityPageResult.getRecords().stream().map(entity -> {
            Document document = new Document();
            BeanUtil.copyProperties(entity, document);
            return document;
        }).collect(Collectors.toList());

        return PageResult.of(entityPageResult, documents);

    }


}
